<?php

/**
 * This is the model class for table "sd.comments".
 *
 * The followings are the available columns in table 'sd.comments':
 * @property string $id
 * @property string $reply_to
 * @property string $target_type
 * @property string $target_id
 * @property string $user_id
 * @property string $user_name
 * @property string $date_add
 * @property string $date_moderation
 * @property string $comment
 * @property string $status
 * @property string $ip
 * @property string $user_agent
 *
 * The followings are the available model relations:
 * @property Users $user
 */
class Comments extends CBaseActiveRecord
{
	public $childNodes = array();



	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sd.comments';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('target_type, target_id, date_add, comment', 'required'),
			array('reply_to, target_id, user_id', 'length', 'max'=>10),
			array('target_type', 'length', 'max'=>20),
			array('user_name', 'length', 'max'=>45),
			array('email', 'length', 'max'=>100),
			array('status', 'length', 'max'=>14),
			array('ip', 'length', 'max'=>15),
			array('user_agent', 'length', 'max'=>255),
			array('date_moderation', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, reply_to, target_type, target_id, user_id, user_name, email, date_add, date_moderation, comment, status, ip, user_agent', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'reply_to' => 'Reply To',
			'target_type' => 'Target Type',
			'target_id' => 'Target',
			'user_id' => 'User',
			'user_name' => 'User Name',
			'email' => 'Email',
			'date_add' => 'Date Add',
			'date_moderation' => 'Date Moderation',
			'comment' => 'Comment',
			'status' => 'Status',
			'ip' => 'Ip',
			'user_agent' => 'User Agent',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('reply_to',$this->reply_to,true);
		$criteria->compare('target_type',$this->target_type,true);
		$criteria->compare('target_id',$this->target_id,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('date_add',$this->date_add,true);
		$criteria->compare('date_moderation',$this->date_moderation,true);
		$criteria->compare('comment',$this->comment,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('ip',$this->ip,true);
		$criteria->compare('user_agent',$this->user_agent,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Comments the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function getByTargetId($targetId)
	{
		if($targetId < 1)
			return array();

		$oCriteria = $this->getDbCriteria();
		$oCriteria->addInCondition('target_id', (array)$targetId);

		$oRowset = $this->findAll($oCriteria);

		$oRowset = $this->toTree($oRowset, 'id', 'reply_to');

		return $oRowset;
	}

}
